package atessitura;
import java.applet.AudioClip;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;

public class EFrame extends JFrame implements MouseListener
{
	public static final Color[] backgroundColors={Color.black, Color.BLUE, Color.pink, Color.red, Color.green, Color.yellow, Color.white, Color.orange, Color.magenta};
	public static final String[] strings={"Hello Eden!!", 
		"I love you :)", 
		"You write good codes", 
		"Do you like to dance?", 
		"Will you go to White Out with me?", 
		"Pretty please?", "Don't make me beg", 
		"How about now?", 
		"What if I said... by saying yes... ", "You will be awarded a pet fire breathing dragon!", 
		"I like you",
		"Last year I won a Taper parking spot. ", " This year, I want to win your heart",
		"I do hope you're enjoying this!",
		"I like cheese."};
	private JPanel background;
	private int myInt;
	private Timer time;
	private int count;
	
	public EFrame()
	{
		this(0);
	}
	
	public EFrame(int i)
	{
		
			
		super(strings[i%strings.length]+" <3 you!");
		if (i ==strings.length-1)
		{
			Object[] options = {"Yes",
                    "No",
                    "I hate you"};
			int n = JOptionPane.showOptionDialog(new JFrame(),
					"So what do you say?",
							"File exists",
							JOptionPane.YES_NO_CANCEL_OPTION,
							JOptionPane.QUESTION_MESSAGE,
							null,
							options,
							options[2]);
			if (n==0)
			{
				JOptionPane.showMessageDialog(new JFrame(), "Yay, I love you!");
			}
			if (n==1)
			{
				JOptionPane.showMessageDialog(new JFrame(), "Damn... Can we still be friends?");
			}
			if (n==2)
			{
				JOptionPane.showMessageDialog(new JFrame(), "I hate you too!");
			}
			
			time.start();
			return;
		}
		setVisible(true);
		count=i;
		myInt=count%strings.length;
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		int randomWidth=(int)(Math.random()*500);
		int randomHeight=(int)(Math.random()*500);
		int randomJWidth=(int)(Math.random()*200+800);
		int randomJHeight=(int)(Math.random()*300+200);
		if (myInt%strings.length == 4)
		{
			//randomWidth = 500;
			//randomHeight = 500;
			randomJWidth = 1000;
			randomJHeight = 500;
		}
		if (myInt%strings.length == 8 || myInt%strings.length == 9 || myInt%strings.length == 11 || myInt%strings.length ==12)
		{
			//randomWidth = 800;
			//randomHeight = 500;
			randomJWidth = 1300;
			randomJHeight = 500;
		}
		setBounds(randomWidth, randomHeight, randomJWidth, randomJHeight);
		background=new JPanel();
		setContentPane(background);
		setBackground(backgroundColors[(int)(Math.random()*(backgroundColors.length-1))]);
		this.addMouseListener(this);
		time=new Timer(500, new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				changeBackgroundColor();
				
			}
			
		});
		time.start();
		playSound();
		repaint();
	}
	
	public void paint(Graphics g)
	{
		drawStringEverywhere(g);
	}
	
	public void playSound()
	{
		try {
			URL file = new File("src/406__tictacshutup__click-1-d.wav").toURI().toURL();
			//URL file = new File("sounds/72129__kizilsungur__sweetalertsound5.wav").toURI().toURL();		
			AudioInputStream audioIn = AudioSystem.getAudioInputStream(file);
			Clip clip = AudioSystem.getClip();
			clip.open(audioIn);
			clip.start();
			} catch (UnsupportedAudioFileException e) {
			e.printStackTrace();
			} catch (IOException e) {
			e.printStackTrace();
			} catch (LineUnavailableException e) {
			e.printStackTrace();
			}
	}
	
	public void changeBackgroundColor()
	{
		Color current=this.getBackground();
		Color changed=backgroundColors[(int)(Math.random()*(backgroundColors.length-1))];
		while(current.equals(changed))
		{
			changed=backgroundColors[(int)(Math.random()*(backgroundColors.length-1))];
		}
		this.setBackground(changed);
	}
	
	public void drawStringEverywhere(Graphics g)
	{
		g.setColor(Color.black);
		float size=50;
		g.setFont(g.getFont().deriveFont(size));
		g.drawString(strings[myInt], 100, (int)this.getSize().getHeight()/2);
		if (myInt%strings.length == strings.length-1)
		{
			
			return;

		}
		if (myInt%strings.length == 4)
		{
			BufferedImage image;
			try {
				image = ImageIO.read(new File("IMG_1290.JPG"));				
				g.drawImage(image, 100, (int) this.getSize().getHeight()/2, image.getWidth()/10,image.getHeight()/10, null);
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return;
		}
		if (myInt%strings.length == 8 || myInt%strings.length == 9 || myInt%strings.length == 11 || myInt%strings.length ==12)
		{
			
			
			return;
		}
		
		for (int i=0;i<25;i++)
		{
			size=(float)(Math.random()*50);
			int randomWidth=(int)(Math.random()*this.getSize().getWidth()-size);
			int randomHeight=(int)(Math.random()*this.getSize().getHeight());
			g.setColor(backgroundColors[(int)(Math.random()*(backgroundColors.length-1))+1]);
			g.setFont(g.getFont().deriveFont(size));
			g.drawString(strings[myInt], randomWidth, randomHeight);
		}
	}

	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
		//myOpened.add(new EFrame());
		new EFrame((myInt+1)%strings.length);
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		//new EFrame((myInt+1)%strings.length);
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		//new EFrame((myInt+1)%strings.length);
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}
